package tree;

public class BianLiTree {
    public static void main(String[] args) {
        Node root = new Node(5);
        root.left = new Node(3);
        root.right = new Node(3);
        root.left.left = new Node(9);
        root.left.right = new Node(8);
        root.right.left = new Node(8);
        root.right.right = new Node(9);

        //先序遍历
        //printXianXu(root);

        //中序遍历
       // printZhongXu(root);

        //后序遍历
        printHouXu(root);
    }

    private static void printHouXu(Node root) {
        if (root == null){
            return;
        }
        printXianXu(root.left);
        printXianXu(root.right);
        System.out.println(root.value);
    }

    private static void printZhongXu(Node root) {
        if(root == null){
            return;
        }
        printZhongXu(root.left);
        System.out.println(root.value);
        printZhongXu(root.right);
    }

    /**
     * 遍历思路
     *       先打印根节点
     *       再递归打印左子树
     *       最后递归处理右子树*/
    private static void printXianXu(Node root) {
        if (root == null){
            return;
        }
        System.out.println(root.value);
        printXianXu(root.left);
        printXianXu(root.right);
    }

    //二叉树节点类
    static class Node{
        int value;
        Node left;
        Node right;
        public Node(int value) {
            this.value = value;
        }
    }
}